class Project < ActiveRecord::Base
   has_many :tasks, :dependent => :destroy # if a project is destroyed, the tasks too
   has_and_belongs_to_many :users
   
   validates_presence_of :name
   #validates_uniqueness_of :name
   
   # return the % of tasks done
   def progress
    total = tasks.size
    return 100 if total == 0
    done = tasks.inject(0) { |sum,task| task.done==1 ? sum+1 : sum}
    (done*100)/total
   end
end
